package com.wanglin.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanglin.reggie.common.CustomException;
import com.wanglin.reggie.dto.SetmealDto;
import com.wanglin.reggie.entity.Setmeal;
import com.wanglin.reggie.entity.SetmealDish;
import com.wanglin.reggie.mapper.SetmealMapper;
import com.wanglin.reggie.service.SetmealDishService;
import com.wanglin.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * SetmealServiceImpl
 *
 * @author 王林
 * @version 1.0
 * @description
 * @date 2025/5/29 03:29
 * @projectName reggie_take_out
 * @package com.wanglin.reggie.service.impl
 * @className com.wanglin.reggie.service.impl.SetmealServiceImpl
 * @copyright Copyright 2020 Theofanis, Wang Lin All rights reserved.
 */
@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存套餐信息
        this.save(setmealDto);
        // 将套餐菜品信息转换为SetmealDish对象
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream().map((item) -> {
            // 设置套餐ID
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        // 批量保存套餐菜品信息
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 回显套餐数据：根据套餐id查询套餐
     * @return
     */
    @Override
    @Transactional
    public SetmealDto getDate(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        //在关联表中查询，setmealdish
        queryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);

        if (setmeal != null){
            BeanUtils.copyProperties(setmeal,setmealDto);
            List<SetmealDish> list = setmealDishService.list(queryWrapper);
            setmealDto.setSetmealDishes(list);
            return setmealDto;
        }
        return null;
    }

    /**
     * 根据套餐id修改售卖状态
     * @param status
     * @param ids
     */
    @Override
    public void updateSetmealStatusById(Integer status,  List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(ids !=null,Setmeal::getId,ids);
        List<Setmeal> list = this.list(queryWrapper);

        for (Setmeal setmeal : list) {
            if (setmeal != null){
                setmeal.setStatus(status);
                this.updateById(setmeal);
            }
        }
    }

    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        // 创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 判断ids是否为空，如果不为空，则将ids中的元素添加到queryWrapper中
        queryWrapper.in(ids != null,Setmeal::getId,ids);
        // 设置查询条件，status为1
        queryWrapper.eq(Setmeal::getStatus,1);
        // 查询符合条件的记录数
        int count = this.count(queryWrapper);
        // 如果记录数大于0，则抛出异常
        if (count > 0){
            // 套餐正在售卖中，不能删除
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        // 根据ids删除记录
        this.removeByIds(ids);
        // 创建LambdaQueryWrapper对象
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 判断ids是否为空，如果不为空，则将ids中的元素添加到lambdaQueryWrapper中
        lambdaQueryWrapper.in(ids != null,SetmealDish::getSetmealId,ids);
        // 根据lambdaQueryWrapper删除记录
        setmealDishService.remove(lambdaQueryWrapper);
    }
}
